package com.tancire.wxapp;

import com.alibaba.fastjson2.JSONObject;
import com.tancire.domain.SnsBean;
import com.tancire.net.NetRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import static com.tancire.consts.WxappConst.OPEN_ID_URL;

/**
 * 微信小程序 接口调用 实现类
 *
 * @author chenchao
 * @date 2024年1月12日
 */
@Component("wxappRemoteNet")
public class WxappRemoteNet implements OpenRemote {

    @Value("${wx.appid}")
    private String appid;

    @Value("${wx.secret}")
    private String secret;

    /**
     * 获取微信小程序OPEN_ID
     *
     * @param code
     * @return
     */
    @Override
    public SnsBean getOpenId(String code) {
        String url = OPEN_ID_URL + "?appid=" + appid + "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";
        JSONObject resBody = NetRequest.request(url);
        System.out.println("========================================");
        System.out.println(resBody);
        // session_key
        String openid = resBody.getString("openid");
        String sessionKey = resBody.getString("session_key");
        SnsBean res = new SnsBean();
        res.setOpenid(openid);
        res.setSessionKey(sessionKey);
        return res;
    }
}
